<template>

  <el-row>
    <el-col :span="8" style="padding-right:5px">
      <el-card class="box-card" shadow="hover">
        <div class="user">
          <img :src="userimg" alt="">
          <div class="userinfo">
            <p class="name">老谭</p>
            <p class="access">管理员</p>
          </div>
        </div>
        <div class="login-info">
          <p>上次登录时间：<span>2022-12-28</span></p>
          <p>上次登录地点：<span>广东</span></p>
        </div>
      </el-card>
      <el-card shadow="hover" style="margin-top:10px;height: 170px;">
        <el-table :data="tableData" style="width: 100%">
          <!-- v-for也可以渲染对象，key代表数据名称，val代表数据 -->
          <el-table-column v-for="(val,key) in tableLabel" :prop="key" :label="val" :key="key" />
        </el-table>
      </el-card>
    </el-col>
    <el-col :span="16" style="padding-left:5px">
      <div class="num">
        <el-card shadow="hover" v-for="item in countData" :key="item.name" :body-style="{display:'flex',padding:0}">
          <i class="icon" :class="`el-icon-${item.icon}`" :style="{background:item.color}"></i>
          <div class="detail">
            <p class="price">￥{{ item.value }}</p>
            <p class="desc">{{ item.name }}</p>
          </div>
        </el-card>
      </div>
      <!-- 折线图 -->
      <el-card style="height:160px; padding-top: 10px;" :body-style="{padding:0}">
        <div ref="echarts1" style="height:180px"></div>
      </el-card>
      <div class="graph">
        <el-card style="height:110px">
          <div ref="echarts2" style="height:110px"></div>
        </el-card>
        <el-card style="height:110px">
          <div ref="echarts3" style="height:110px"></div>
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>
<script>
import { getData } from '../api'
import * as echarts from 'echarts'
export default {
  data() {
    return {
      //用户头像
      userimg: 'https://ts1.cn.mm.bing.net/th/id/R-C.aff2319c74a600929a3a97321c4d6e1c?rik=s468D3gw3bhZ1w&riu=http%3a%2f%2ftupian.qqw21.com%2farticle%2fUploadPic%2f2017-2%2f20172262062516908.jpg&ehk=DLY6M1onZcxnhBW5yszZ%2b6v%2bmAiwmH6fahBVBOAKWv0%3d&risl=&pid=ImgRaw&r=0',
      //左下数据
      tableData: [],
      tableLabel: {
        name: '课程',
        todayBuy: '今日购买',
        monthBuy: '本月购买',
        totalBuy: '总购买'
      },
      //右上数据
      countData: [
        {
          name: '今日支付订单',
          value: 1234,
          icon: 'success',
          color: '#2ec7c9'
        },
        {
          name: '今日收藏订单',
          value: 210,
          icon: 'star-on',
          color: '#ffb980'
        },
        {
          name: '今日未支付订单',
          value: 1234,
          icon: 's-goods',
          color: '#5ab1ef'
        },
        {
          name: '本月支付订单',
          value: 1234,
          icon: 'success',
          color: '#2ec7c9'
        },
        {
          name: '本月收藏订单',
          value: 210,
          icon: 'star-on',
          color: '#ffb980'
        },
        {
          name: '本月未支付订单',
          value: 1234,
          icon: 's-goods',
          color: '#5ab1ef'
        }
      ]
    }
  },
  mounted() {
    getData().then(({ data }) => {
      //解构赋值
      const { tableData } = data.data
      // console.log(tableData);
      this.tableData = tableData

      //折线图
      //基于准备好的dom，初始化echarts
      const echarts1 = echarts.init(this.$refs.echarts1)
      //指定图标的配置和数据
      var echartsoption = {}
      //处理数据xAXis
      const { orderData, userData,videoData } = data.data
      const xAxis = Object.keys(orderData.data[0])
      const xAxisData = {
        data: xAxis
      }
      const xAxisDate = {
        data: orderData.date
      }
      //x轴数据
      echartsoption.xAxis = xAxisDate 
      //y轴数据
      echartsoption.yAxis = {}
       //顶部说明数据
      echartsoption.legend = xAxisData
      //表中数据
      echartsoption.series = []
      xAxis.forEach(key => {
        echartsoption.series.push({
          name: key,
          data: orderData.data.map(item => item[key]),
          type: 'line'
        })
      })
      console.log(data);
      //使用刚指定的配置项和数据显示图表
      echarts1.setOption(echartsoption)

      //柱状图
      //基于准备好的dom，初始化echarts
      const echarts2 = echarts.init(this.$refs.echarts2)
      //指定图标的配置和数据
      const echarts2option = {
        legend: {
          // 图例文字颜色
          textStyle: {
            color: '#333'
          }
        },
        grid: {
          left: '20%'
        },
        // 提示框
        tooltip: {
          trigger: 'axis'
        },
        xAxis: {
          type: 'category', // 类目轴
          data: userData.map(item => item.date),
          axisLine: {
            lineStyle: {
              color: '#17b3a3'
            }
          },
          axisLabel: {
            interval: 0,
            color: '#333'
          }
        },
        yAxis: [
          {
            type: 'value',
            axisLine: {
              lineStyle: {
                color: '#17b3a3'
              }
            }
          }
        ],
        color: ['#2ec7c9', '#b6a2de'],
        series: [
          {
            name: '新增用户',
            data: userData.map(item => item.new),
            type: 'bar'
          },
          {
            name: '活跃用户',
            data: userData.map(item => item.active),
            type: 'bar'
          }
        ],
      }
      echarts2.setOption(echarts2option)
      
      //饼状图
      //基于准备好的dom，初始化echarts
      const echarts3 = echarts.init(this.$refs.echarts3)
      //指定图标的配置和数据
      const echarts3option = {
        tooltip: {
            trigger: "item",
          },
          color: [
            "#0f78f4",
            "#dd536b",
            "#9462e5",
            "#a6a6a6",
            "#e1bb22",
            "#39c362",
            "#3ed1cf",
          ],
          series: [
            {
              data:videoData,
              type:'pie'
            }
          ],
      }
      echarts3.setOption(echarts3option)
    })
  }
}
</script>
<style lang="less" scoped>
.user {
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ccc;
  padding-bottom: 10px;
  margin-bottom: 10px;
  img {
    margin-right: 40px;
    width: 100px;
    height: 100px;
    border-radius: 50%;
  }
  .userinfo {
    .name {
      font-size: 24px;
      margin-bottom: 10px;
    }
    .access {
      font-size: 12px;
      color: #999999;
    }
  }
}
.login-info {
  p {
    line-height: 28px;
    font-size: 14px;
    color: #999999;
    span {
      color: #999999;
      margin-left: 30px;
    }
  }
}
.num {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;

  .icon {
    width: 50px;
    height: 50px;
    font-size: 20px;
    text-align: center;
    line-height: 50px;
    color: #fff;
  }
  .detail {
    margin-left: 5px;
    display: flex;
    flex-direction: column;
    padding-top: 5px;
    .price {
      font-size: 17px;
    }
    .desc {
      font-size: 12px;
      color: #999999;
      text-align: center;
    }
  }
  .el-card {
    width: 32%;
    margin-bottom: 10px;
  }
}
.graph {
  display: flex;
  justify-content: space-between;
  .el-card {
    margin-top: 10px;
    width: 48%;
  }
}
</style>